package com.hiabr.iep.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String FORMAT_YYYYMMDD = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String FORMAT_YYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    public final static String FORMAT_YYYMMDDHHMMSS_NOSTYLE = "yyyyMMddHHmmss";

    /**
     * 时间格式(yyyy-MM-dd HH:mm)
     */
    public final static String FORMAT_YYYMMDDHHMM = "yyyy-MM-dd HH:mm";


    /**
     * 日期增加年
     *
     * @param date
     * @param years
     * @return
     */
    public static Date addYear(Date date, int years) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, years);
        return cal.getTime();
    }

    /**
     * 日期增加月份
     *
     * @param date
     * @param months
     * @return
     */
    public static Date addMonth(Date date, int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        return cal.getTime();
    }

    /**
     * 日期增加天数
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDay(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }

    /**
     * 日期增加小时
     *
     * @param date
     * @param hours
     * @return
     */
    public static Date addHour(Date date, int hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        return cal.getTime();
    }

    /**
     * 日期增加分钟
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Date addMinute(Date date, int minutes) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minutes);
        return cal.getTime();
    }

    /**
     * 日期增加秒
     *
     * @param date
     * @param seconds
     * @return
     */
    public static Date addSecond(Date date, int seconds) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, seconds);
        return cal.getTime();
    }

    /**
     * 获得今天23:59:59.999
     */
    public static Date getEndOfDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, -1);
        return calendar.getTime();
    }

    /**
     * 获得今天00:00:00.000
     */
    public static Date getBeginOfDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得月初
     *
     * @param date 日期
     * @return {@link Date}
     */
    public static Date getBeginOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取月末
     *
     * @param date 日期
     * @return {@link Date}
     */
    public static Date getEndOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, -1);
        return calendar.getTime();
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {

        if (nowTime.getTime() == beginTime.getTime() || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否在给定小时内
     *
     * @param end  - start > hour
     * @param hour
     * @return
     * @throws Exception
     */
    public static boolean judgmentDate(Date start, Date end, Integer hour) {
        long cha = end.getTime() - start.getTime();
        if (cha < 0) {
            return false;
        }
        double result = cha * 1.0 / (1000 * 60 * 60);
        if (result > hour) {
            return true;//是大于 hour 小时
        } else {
            return false;
        }
    }


    /**
     * 格式化字符串为日期
     *
     * @param date
     * @param format
     * @return
     */
    public static Date parseDate(String date, String format) {
        try {
            return new SimpleDateFormat(format).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 结果 Fri Dec 31 23:59:59 CST 9999
     * 获取9999-12-31 23:59:59日期
     *
     * @return
     */
    public static Date get9999() {
        try {
            return new SimpleDateFormat(FORMAT_YYYMMDDHHMMSS).parse("9999-12-31 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取两个日期之间的天数
     *
     * @return
     */
    public static Long getDay(Date startDate, Date endDate, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            Date start = dateFormat.parse(dateFormat.format(startDate));
            Date end = dateFormat.parse(dateFormat.format(endDate));
            long day = (end.getTime() - start.getTime()) / 24 / 60 / 60 / 1000;
            return day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getUnixTimestamp(Date date) {
        if (date == null) {
            return 0;
        }
        return (int) (date.getTime() / 1000);
    }

    public static void main(String[] args) {
        System.out.println(get9999());

        System.out.println(getEndOfDate(new Date()));
        System.out.println(getBeginOfMonth(new Date()));
        System.out.println(getEndOfMonth(new Date()));

    }
}
